package cn.itcast.controller.dept;

import cn.itcast.base.BaseController;
import cn.itcast.domain.Dept;
import cn.itcast.service.dept.DeptService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/dept")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @RequestMapping(name = "pageHelper插件分页查询部门",value = "/list")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        System.out.println("44444444444");
        PageInfo pb = deptService.findAll(pageNum, pageSize, company_id);
        request.setAttribute("pb",pb);
        return "system/dept/dept-list";

    }

    @RequestMapping(name = "到新建部门页面",value = "/toAdd")
    public String toAdd(){
        //查询所有部门
        List<Dept> list=deptService.findAllDept(company_id);
        request.setAttribute("deptList",list);
        return "system/dept/dept-add";
    }

    @RequestMapping(name = "部门保存/部门更新",value = "/edit")
    public String edit(Dept dept){

        //指定当前部门是属于哪个公司的
        dept.setCompanyId(company_id);
        dept.setCompanyName(company_name);

        if(UtilFuns.isEmpty(dept.getId())){
            // 新增
            dept.setId(UUID.randomUUID().toString());
            deptService.save(dept);
        }else{
            // 更新
            deptService.update(dept);

        }

        return "redirect:/system/dept/list.do";
    }



    @RequestMapping(name = "到修改部门页面回显数据",value = "/toUpdate")
    public String toUpdate(String id){
        Dept dept=deptService.findById(id);
        request.setAttribute("dept",dept);

        List<Dept> list=deptService.findAllDept(company_id);
        request.setAttribute("deptList",list);
        return "system/dept/dept-update";
    }


    @RequestMapping(name = "部门数据删除",value = "/delete")
    public String delete(String id){

        deptService.delete(id);

        return "redirect:/system/dept/list.do";
    }




}
